<?php

namespace App\Admin\Controllers;

use App\Models\Collect;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CollectsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $collect = new Collect;
        $grid = new Grid($collect);
        $grid->model()->withCount('movies')->with('author')->orderBy('updated_at', 'desc');
        $grid->id('序号')->sortable();
        $grid->title('标题');
        $grid->column('author', '创建者')->display(function() {
            return '<a href="' . route('members.show', $this->author->id) . '">' . $this->author->name . '</a>';
        });
        $grid->introduction('简介');
        $grid->column('movies_count', '包含电影数')->sortable();
        $grid->created_at('创建时间');
        $grid->updated_at('最后更新');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Collect::findOrFail($id));

        $show->id('ID');
        $show->title('标题');
        $show->introduction('简介');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Collect);

        $form->display('id', '编号');
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '最后更新');
        $form->divider('编辑信息');
        $form->text('title', '标题')->autofocus();
        $form->textarea('introduction', '简介');


        return $form;
    }
}
